﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using sdpmt.minitabinterface;
using sdpmt.filereader;
using System.Windows.Media.Imaging;

namespace sdpmt
{
    public partial class MainForm : Form
    {
        private string m_FilePath;
        
        /// <summary>
        /// Public CTOR
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the click of the Browse button
        /// </summary>
        /// <param name="sender">Sender of the event, the browse button</param>
        /// <param name="e">Event arguments</param>
        private void m_BrowseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            f.Filter = "CSV File|*.csv";
            f.ShowDialog();
            m_FilePath = f.FileName;

            this.textBox1.Text = m_FilePath;            

        }//end m_BrowseButton_Click

        /// <summary>
        /// Handles the click of the Scatterplot button
        /// </summary>
        /// <param name="sender">Sender of the event, the Scatterplot button</param>
        /// <param name="e">Event arguments</param>
        private void m_ScatterplotButton_Click(object sender, EventArgs e)
        {
            MinitabBroker mtb = new MinitabBroker();

            System.Collections.Generic.SortedDictionary<DateTime, double> list =
                CsvReader.Read(m_FilePath, dateTimePicker1.Value.Date, dateTimePicker2.Value.Date);

            pictureBox1.Image = mtb.GetFittedLinePlot(list);
        }

        /// <summary>
        /// Handles the click of the Trend button
        /// </summary>
        /// <param name="sender">Sender of the event, the Trend button</param>
        /// <param name="e">Event arguments</param>
        private void button1_Click(object sender, EventArgs e)
        {
            MinitabConstants.TrendModelType trendType = MinitabConstants.TrendModelType.LINEAR;
            int forecast = 0;

            if (string.IsNullOrEmpty(this.textBox2.Text)==false)
            {
                forecast = int.Parse(textBox2.Text);
            }

            if (this.comboBox1.SelectedValue != null)
            {
                switch (this.comboBox1.SelectedValue.ToString())
                {
                    case "Quadratic":
                        trendType = MinitabConstants.TrendModelType.QUADRATIC;
                        break;
                    case "S-Curve":
                        trendType = MinitabConstants.TrendModelType.SCURVE;
                        break;
                    case "Linear":
                        trendType = MinitabConstants.TrendModelType.LINEAR;
                        break;
                    default:
                        trendType = MinitabConstants.TrendModelType.LINEAR;
                        break;
                }
            }
            
            MinitabBroker mtb = new MinitabBroker();

            System.Collections.Generic.SortedDictionary<DateTime, double> list =
                CsvReader.Read(m_FilePath, dateTimePicker1.Value.Date, dateTimePicker2.Value.Date);

            pictureBox1.Image = mtb.GetForecast(list, forecast, trendType).Item1;
            
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            string t = (sender as TextBox).Text;
            int i;

            int.TryParse(t, out i);
            
            if(string.IsNullOrEmpty(t) == false)
            {
                if (i > 0)
                {
                    button1.Enabled = true;
                }
                else
                {
                    button1.Enabled = false;
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    
    }//end class MainForm

}//end namespace
